public class question_28 {
}
class Solution_28 {
    public int strStr(String haystack, String needle) {
//        先考虑特殊情况
        if(haystack==null||needle==null||(haystack.length()<needle.length())){
            return -1;
        }
        char[] hay = haystack.toCharArray();
        char[] need = needle.toCharArray();
        int len_hay=hay.length;
        int len_need=needle.length();
        //        index初始值是-1，一开始表示没有匹配的，后面找到了再更新

        int index=-1;
        for (int i = 0; i <= len_hay-len_need; i++) {
//            首字母相同再取子串
            if(hay[i]==need[0]){
//                先截取相应长度的字符串
//                String substring = haystack.substring(i, len_need);
                String substring=haystack.substring(i,i+len_need);
                if(substring.equals(needle)){
                    index=i;
                    break;
                }
            }
        }
        return index;
    }

    public static void main(String[] args) {
        Solution_28 solution_28 = new Solution_28();
        solution_28.strStr("abc", "c");

    }
}
